import java.awt.EventQueue;
import java.awt.Image;

import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Font;
import java.awt.Color;
  
public class Phase1Runner {
	
	//Various flags
	int question = 0;
	boolean sampleclicked = false;
	boolean sampleready = true;
	
	private JFrame frame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Phase1Runner window = new Phase1Runner();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Phase1Runner() {
		initialize();
	}
	
	/**
	 * Converts an imported image (any supported) to an icon to be
	 * placed in a label.
	 * @param path Path of the picture in the system.
	 * @return an icon that will be placed in the label.
	 */
	public Icon iconResizer (String path){
			ImageIcon image = new ImageIcon(path);
			Image img = image.getImage();
			Image newimg = img.getScaledInstance(285, 189,  java.awt.Image.SCALE_SMOOTH);
			Icon icon = new ImageIcon(newimg);
			return icon;
		}
	
	/**
	 * Places a new picture in the frame according to
	 * which problem the subject is solving.
	 * @param label the desired label
	 * @param question the question number 0 - 9.
	 */
	public void setWrongIcon(JLabel label){
		switch (question){
		case 0: label.setIcon(iconResizer("kitty.jpg"));break;
		case 1: label.setIcon(iconResizer("kitten_laugh_avatar_picture_32154.jpg"));break;
//		case 2: label.setIcon(iconResizer(/*Path here*/));
//		case 3: label.setIcon(iconResizer(/*Path here*/));
//		case 4: label.setIcon(iconResizer(/*Path here*/));
//		case 5: label.setIcon(iconResizer(/*Path here*/));
//		case 6: label.setIcon(iconResizer(/*Path here*/));
//		case 7: label.setIcon(iconResizer(/*Path here*/));
//		case 8: label.setIcon(iconResizer(/*Path here*/));
//		case 9: label.setIcon(iconResizer(/*Path here*/));
		default: label.setText("WHAT THE FUCK DID YOU DO?");		
		}
		
	}
	
	public void setCorrectIcon(JLabel label){
		switch (question){
		case 0: label.setIcon(iconResizer("kitten_laugh_avatar_picture_32154.jpg"));break;
		case 1: label.setIcon(iconResizer("442416_4.jpg"));break;
//		case 2: label.setIcon(iconResizer(/*Path here*/));
//		case 3: label.setIcon(iconResizer(/*Path here*/));
//		case 4: label.setIcon(iconResizer(/*Path here*/));
//		case 5: label.setIcon(iconResizer(/*Path here*/));
//		case 6: label.setIcon(iconResizer(/*Path here*/));
//		case 7: label.setIcon(iconResizer(/*Path here*/));
//		case 8: label.setIcon(iconResizer(/*Path here*/));
//		case 9: label.setIcon(iconResizer(/*Path here*/));	
		default: label.setText("WHAT THE F*** DID YOU DO?");
		}
		
	}
	
	private void newproblem(JLabel label) {
		question++;
		switch (question){
		case 0: label.setText("Cat");break;
		case 1: label.setText("Horse");break;
//		case 2: label.setIcon(iconResizer(/*Path here*/));
//		case 3: label.setIcon(iconResizer(/*Path here*/));
//		case 4: label.setIcon(iconResizer(/*Path here*/));
//		case 5: label.setIcon(iconResizer(/*Path here*/));
//		case 6: label.setIcon(iconResizer(/*Path here*/));
//		case 7: label.setIcon(iconResizer(/*Path here*/));
//		case 8: label.setIcon(iconResizer(/*Path here*/));
//		case 9: label.setIcon(iconResizer(/*Path here*/));
		default: label.setText("WHAT THE F*** DID YOU DO?");
		}
	}

	public static void oneSec(int n) {		         
		long t0, t1;
		t0 =  System.currentTimeMillis();
		do{
			t1 = System.currentTimeMillis();
			}
		while (t1 - t0 < n);
	}
	
	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 997, 577);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		final JLabel lblComparisonCorrect = new JLabel("");
		lblComparisonCorrect.setLabelFor(frame);
		lblComparisonCorrect.setHorizontalAlignment(SwingConstants.CENTER);
		lblComparisonCorrect.setForeground(new Color(46, 139, 87));
		lblComparisonCorrect.setFont(new Font("Courier New", Font.BOLD, 32));

		final JLabel lblComparisonWrong = new JLabel("");
		lblComparisonWrong.setHorizontalAlignment(SwingConstants.CENTER);
		lblComparisonWrong.setForeground(new Color(255, 0, 0));
		lblComparisonWrong.setFont(new Font("Courier New", Font.BOLD, 38));
	
		final JLabel lblSample = new JLabel("Cat");
		lblSample.setHorizontalAlignment(SwingConstants.CENTER);
		lblSample.setFont(new Font("Segoe Print", Font.BOLD, 38));
		
		GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(71)
					.addComponent(lblComparisonCorrect, GroupLayout.PREFERRED_SIZE, 285, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, 264, Short.MAX_VALUE)
					.addComponent(lblComparisonWrong, GroupLayout.PREFERRED_SIZE, 285, GroupLayout.PREFERRED_SIZE)
					.addGap(76))
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(324)
					.addComponent(lblSample, GroupLayout.PREFERRED_SIZE, 285, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(372, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(lblComparisonWrong, GroupLayout.PREFERRED_SIZE, 189, GroupLayout.PREFERRED_SIZE)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblSample, GroupLayout.PREFERRED_SIZE, 189, GroupLayout.PREFERRED_SIZE)
							.addGap(96)
							.addComponent(lblComparisonCorrect, GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE)))
					.addGap(54))
		);
		frame.getContentPane().setLayout(groupLayout);
		
		final Runnable setCorrect = new Runnable (){

			public void run() {
				lblComparisonCorrect.setIcon(null);
				lblComparisonCorrect.setText("Correct!"); 	
			}
			
		};
		
		lblSample.addMouseListener(new MouseAdapter() {
			//Mouse events for the label.
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (arg0.getButton() == 1){
					sampleclicked = true;
					sampleready = false;
					//Check for compare to sample
					//do compare to sample
					setCorrectIcon(lblComparisonCorrect);
					setWrongIcon(lblComparisonWrong);
					//do delayed matched to sample.
					/* (if flag for delayed matched to sample is set)
					 * lblComparisonCorrect.setIcon(iconResizer("C:\\Users\\Joan\\workspace\\Phase1Milestone1\\kitty.jpg"));
					 * lblComparisonWrong.setIcon(iconResizer("C:\\Users\\Joan\\workspace\\Phase1Milestone1\\kitten_laugh_avatar_picture_32154.jpg"));
					 * lblSample.setIcon(null);
					 * lblComparisonCorrect.updateUI();
					 */
				}
			}
		});
		lblComparisonCorrect.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if(arg0.getButton() == 1 && sampleclicked){
					sampleclicked = false;
					//Correct selection set of tasks.
					//SwingUtilities.invokeLater(setCorrect);
					//wait
					lblComparisonCorrect.setText(null);
					lblComparisonCorrect.setIcon(null);
					lblComparisonWrong.setIcon(null);
					sampleready= true;
					newproblem(lblSample);
				}
			}
		});
		lblComparisonWrong.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if(arg0.getButton() == 1 && sampleclicked){
					sampleclicked = false;
				//	frame.repaint();
				//	Incorrect selection set of tasks.
				//	lblComparisonWrong.setIcon(null);
				//	lblComparisonWrong.setText("Wrong!");
				//	oneSec(2000);
					lblComparisonWrong.setText(null);
					lblComparisonCorrect.setIcon(null);
					lblComparisonWrong.setIcon(null);
					sampleready= true;
					newproblem(lblSample);
				}
			}
		});
	}
}
